<template>
    <div class="login">
        <van-nav-bar
          title="登录页面"
          left-text="返回"
          left-arrow
          @click-left="$router.push('/home')"
        />
      <van-form @submit="onSubmit">
        <van-field
          v-model="phone"
          name="phone"
          label="电话号码"
          placeholder="请输入电话号码"
          :rules="[{ required: true, message: '请输入电话号码' }]"
        />
        <div class="code">
            <van-field
            v-model="code"
            name="code"
            label="验证码"
            placeholder="请输入验证码"
            :rules="[{ required: true, message: '请输入验证码' }]"/>
            <!-- 获取验证码的按钮 -->
            <van-button class="codeBtn" type="info" native-type='button' @click="handelGetCode">获取验证码(6)</van-button>
        </div>
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit" @click="handleLogin">登录</van-button>
        </div>
      </van-form>
    </div>
</template>

<script>
import {
  getCodeApi
  // loginApi
} from '@/api/userLoginApi'
// import { Toast } from 'vant'
export default {
  data () {
    return {
      phone: '',
      code: ''
    }
  },
  methods: {
    async onSubmit (values) {
      const result = await this.$store.dispatch('user/LoginAction', values)
      console.log(result, '布尔值')
      if (result) {
        this.$toast({
          message: '登录成功',
          type: 'success'
        })
        // this.$router.push('/home')
      } else {
        this.$toast({
          message: '用户名或者验证码错误',
          type: 'fail'
        })
      }
    },
    async handelGetCode () {
      const result = await getCodeApi(this.phone)
      console.log(result, '验证码--')
      this.$toast({
        message: '验证码：' + result.code,
        type: 'success'
      })
    },
    async handleLogin () {
      // const result = await loginApi({ phone: this.phone, code: this.code })
      // this.$toast({
      //   message: '登录成功',
      //   type: 'success'
      // })
      // console.log(result, '登录')
      // this.$store.dispatch('user/LoginAction', result)
      // this.$router.push('/home')
    }
  }
}
</script>

<style lang="less" scoped>
.code{
  display: flex;
  >.codeBtn{
    width: 150px;
    font-size: 12px;
    line-height: 100%;
    text-align: center;
    border-radius: 5px;
  }
}
</style>
